package com.example.demo.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;

import com.example.demo.R;
import com.example.demo.fragment.CarFragment;
import com.example.demo.fragment.HomeFragment;
import com.example.demo.fragment.MineFragment;
import com.example.demo.fragment.OrderFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class FragmentMainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_main);

        bottomNavigationView= findViewById(R.id.bottomNavigation);

        loadFragment(new HomeFragment());
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            Fragment selectedFragment = null;

            switch (item.getItemId()) {
                case R.id.nav_home:
                    selectedFragment = new HomeFragment();
                    break;
                case R.id.nav_cart:
                    selectedFragment = new CarFragment();
                    break;
                case R.id.nav_orders:
                    selectedFragment = new OrderFragment();
                    break;
                case R.id.nav_mine:
                    selectedFragment = new MineFragment();
                    break;
            }

            return loadFragment(selectedFragment);


        });




    }

    private boolean loadFragment(Fragment fragment) {
        // 替换当前显示的 Fragment
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, fragment)
                    .commit();
            return true;
        }
        return false;
    }
}